﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;
using Model;
using System.Globalization;
using System.Data;

public partial class Administration_ArticleNew : System.Web.UI.Page
{
    protected Categories current;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                current = Entities.GetCategoryDetails(Convert.ToInt32(Request.QueryString["ID"]));
                litNav.Text = LoadNavigation(current);
                ViewState["CategoryID"] = Convert.ToInt32(Request.QueryString["ID"]);
                calDatePosted.SelectedDate = DateTime.Now;
                trParent.Visible = false;
            }
            else
            {
                trParent.Visible = true;
                // parent category dropdown binder
                DataTable dt = Entities.GetTreeCategories();
                ddlParentCateg.Items.Clear();
                int level = -1;
                RecursiveFillTree(dt, 0, level);

                litNav.Text = LoadNavigation(null);
            }

            ContentPlaceHolder RightContentHolder = (ContentPlaceHolder)Master.FindControl("RightContentHolder");
            RightContentHolder.Visible = false;
        }
    }
    private void RecursiveFillTree(DataTable dtParent, int parentID, int level)
    {
        level++; //on the each call level increment 1
        System.Text.StringBuilder appender = new System.Text.StringBuilder();

        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|");
        }
        if (level > 0)
        {
            appender.Append("_ ");
        }

        DataView dv = new DataView(dtParent);

        dv.RowFilter = string.Format("pid = {0}", parentID);
        dv.Sort = "title asc";
        int i;

        if (dv.Count > 0)
        {
            for (i = 0; i < dv.Count; i++)
            {
                ddlParentCateg.Items.Add(new ListItem(Server.HtmlDecode(appender.ToString() + dv[i]["Title"].ToString()), dv[i]["id"].ToString()));
                RecursiveFillTree(dtParent, int.Parse(dv[i]["id"].ToString()), level);
            }
        }
        level--; //on the each function end level will decrement by 1
    }
    protected string LoadNavigation(Categories categ)
    {
        StringBuilder sb = new StringBuilder();

        if (categ != null)
        {
            Categories current = categ;
            ArrayList nav = new ArrayList();
            nav.Add(current);
            while (current.Parent.CategoryID != 0)
            {
                try
                {
                    current = current.Parent;
                    nav.Add(current);
                }
                catch { break; }
            }
            sb.Append(@" » <a href=""Categories.aspx"">Editare categorii</a>");

            for (int i = nav.Count - 1; i >= 0; i--)
            {
                Categories x = (Categories)nav[i];
                sb.Append(@" » <a href=""categories.aspx?ID=" + x.CategoryID + @""">" + x.Title + @"</a>");

            }
        }
        sb.Append(@" » Adaugare articol nou");
        return sb.ToString();
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string imageFileName = uplImage.FileName;
            if (!string.IsNullOrEmpty(imageFileName))
            {
                uplImage.SaveAs(Utils.GetApplicationSetting(Constants.IMAGES_ARTICLES_PHYSICAL_PATH) + uplImage.FileName);
            }
            string featuredImageFileName = uplFeatured.FileName;
            if (!string.IsNullOrEmpty(featuredImageFileName))
            {
                uplFeatured.SaveAs(Utils.GetApplicationSetting(Constants.IMAGES_ARTICLES_PHYSICAL_PATH) + uplFeatured.FileName);
            }
            int categoryID = (!string.IsNullOrEmpty(ddlParentCateg.SelectedValue))
                    ? Convert.ToInt32(ddlParentCateg.SelectedValue)
                    : (int)ViewState["CategoryID"];
            DateTime datePosted = (calDatePosted.SelectedDate.HasValue) ? calDatePosted.SelectedDate.Value : DateTime.Now;

            Articles addedArticle = Entities.InsertOrUpdateArticle(
                null,
                txtTitle.Text,
                htmlContent.Content,
                categoryID,
                datePosted,
                txtWebDesc.Text,
                txtKeys.Text,
                imageFileName,
                chApproved.Checked,
                chIsFeatured.Checked,
                featuredImageFileName);
            litMessage.Text = @"Articolul a fost adaugat cu succes!<br>Il puteti gasi <a href=""../Article.aspx?ID=""" + addedArticle.ID + @""">AICI</a>";
        }
        catch (Exception ex) { litMessage.Text = ex.ToString(); }
    }
    protected void btnAddImage_Click(object sender, EventArgs e)
    {
        imageEditor.Visible = true;
    }
}
